package leetcode.code0382;

import java.util.Random;

import leetcode.helper.HelpDebug;
import leetcode.helper.tree.ListNode;
//执行用时：14 ms, 在所有 Java 提交中击败了24.36%的用户
//
//内存消耗：40.2 MB, 在所有 Java 提交中击败了84.50%的用户
//
//通过测试用例：8 / 8
public class Solution {

	ListNode head;

	public Solution(ListNode head) {
		this.head = head;
	}

	public int getRandom() {
		int ans = 0;
		ListNode node = head;
		int index = 1;
		while (node != null) {
			if ((int) (Math.random() * index++) == 0) {
				ans = node.val;
			}
			node = node.next;
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution(ListNode.set(HelpDebug.str2array3("[1,2,3,4,5,6]")));
		Random random = new Random();
		for (int i = 0; i < 20; i++) {
//			random.nextInt(0);
//			System.out.println(so.getRandom());
			System.out.println((int) (Math.random() * 1));
		}

	}

}
